package com.zkt.subject.domain.handler.subject;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zkt.subject.common.enums.SubjectInfoTypeEnum;
import com.zkt.subject.domain.convert.SubjectJudgeConverter;
import com.zkt.subject.domain.entity.SubjectUserScore;
import com.zkt.subject.infra.entity.SubjectAnswerBO;
import com.zkt.subject.infra.entity.SubjectInfoBO;
import com.zkt.subject.domain.entity.SubjectOptionBO;
import com.zkt.subject.infra.basic.entity.SubjectJudge;
import com.zkt.subject.infra.basic.service.ISubjectJudgeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2024/10/6
 * @description 判断题的策略类
 **/

@Component
public class JudgeTypeHandler implements SubjectTypeHandler {
	
	@Resource
	private ISubjectJudgeService subjectJudgeService;
	
	/**
	 * 枚举身份的识别
	 *
	 * @return 题目类型的枚举
	 */
	@Override
	public SubjectInfoTypeEnum getHandlerType() {
		return SubjectInfoTypeEnum.JUDGE;
	}
	
	/**
	 * 实际的题目的插入
	 *
	 * @param subjectInfoBO 题目数据
	 */
	@Override
	public void add(SubjectInfoBO subjectInfoBO) {
		if (CollUtil.isEmpty(subjectInfoBO.getOptionList())) {
			return;
		}
		
		// 判断题目的插入
		SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
		SubjectJudge subjectJudge = SubjectJudgeConverter.INSTANCE.convertBoToJudge(subjectAnswerBO);
		subjectJudge.setId(null);
		subjectJudge.setSubjectId(subjectInfoBO.getId());
		
		subjectJudgeService.save(subjectJudge);
	}
	
	/**
	 * 实际的题目的删除
	 *
	 * @param subjectId 题目id
	 */
	@Override
	public void delete(Long subjectId) {
		// 判断题目的插入
		if (subjectId != null) {
			subjectJudgeService.lambdaUpdate()
					.eq(SubjectJudge::getSubjectId, subjectId)
					.remove();
		}
	}
	
	/**
	 * 实际的题目的更新
	 *
	 * @param subjectInfoBO 题目数据
	 */
	@Override
	public void update(SubjectInfoBO subjectInfoBO) {
		if (ObjUtil.isNull(subjectInfoBO.getId()) || CollUtil.isEmpty(subjectInfoBO.getOptionList())) {
			return;
		}
		
		SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
		if (ObjUtil.hasNull(subjectAnswerBO, subjectAnswerBO.getIsCorrect())) {
			return;
		}
		
		this.delete(subjectInfoBO.getId());
		this.add(subjectInfoBO);
	}
	
	/**
	 * 根据题目id查询对应题目信息
	 *
	 * @param subjectId 题目id
	 * @return 题目信息
	 */
	@Override
	public SubjectOptionBO queryBySubjectId(Long subjectId) {
		SubjectJudge subjectJudge = subjectJudgeService.lambdaQuery().eq(SubjectJudge::getSubjectId, subjectId).one();
		if (ObjectUtil.isNull(subjectJudge)) {
			return null;
		}
		SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
		List<SubjectAnswerBO> subjectAnswerList = new LinkedList<>();
		SubjectAnswerBO subjectAnswerBO = SubjectJudgeConverter.INSTANCE.convertJudgeToBo(subjectJudge);
		subjectAnswerList.add(subjectAnswerBO);
		
		subjectOptionBO.setOptionList(subjectAnswerList);
		return subjectOptionBO;
	}
	
	/**
	 * 获取用户得分
	 *
	 * @param subjectInfoBO 用户答案信息
	 * @return 用户得分
	 */
	@Override
	public SubjectUserScore getUserScore(SubjectInfoBO subjectInfoBO) {
		SubjectUserScore subjectUserScore = new SubjectUserScore();
		if (CollUtil.isEmpty(subjectInfoBO.getOptionList())) {
			return subjectUserScore;
		}
		SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
		if (ObjUtil.isNull(subjectAnswerBO) || ObjUtil.isNull(subjectAnswerBO.getIsCorrect())) {
			return subjectUserScore;
		}
		
		SubjectJudge subjectJudge = subjectJudgeService.lambdaQuery().eq(SubjectJudge::getSubjectId, subjectInfoBO.getId()).one();
		if (ObjUtil.isNull(subjectJudge)) {
			return subjectUserScore;
		}
		
		if (subjectAnswerBO.getIsCorrect().equals(subjectJudge.getIsCorrect())) {
			subjectUserScore.setUserScore(subjectInfoBO.getSubjectScore());
		} else {
			subjectUserScore.setUserScore(0);
		}
		
		subjectUserScore.setUserAnswer(subjectAnswerBO.getIsCorrect() == 1 ? "正确" : "错误");
		
		return subjectUserScore;
	}
}
